package j22;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author chengshuai
 * @date 2021/4/11 12:00
 */
public class ContainnerNotSafeDemo {

    public static void main(String[] args) {

//        Set<String> set = Collections.synchronizedSet(new HashSet<>(100000));
//        Set<String> set = new CopyOnWriteArraySet<>();

        HashSet hashSet = new HashSet();

        CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<>();
        ArrayList<Thread> list = new ArrayList<>(100000);

        for (int i = 0; i < 100; i++) {
            final int j = i;
            Thread thread=new Thread(() -> {
                set.add(j+"");
            },String.valueOf(i));
            list.add(thread);
        }

        for (Thread t : list) {
            t.start();
        }

        for (Thread t : list) {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(set.size());

    }
}
